home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: HELP: Code in Header Files
- Date: Fri, 29 Mar 1996 13:02:00 -0500
- Organization: Datalytics, Inc
- Message-ID: <315C2598.4B2A@datalytics.com>
- References: <4jakhm$g3d@news.asu.edu>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- ryan1m@imap2.asu.edu wrote:
- >
- > I have a problem understanding how header files work. If it matters I am
- > using Borland C++ 4.5...
- >
- > The problem is I defined a header file string.h in this I declared a
- > string class. If I put the actual code to the functions in this class
- > into the header file, the compiler does not like it. I looked at other
- > .h files and saw they did not do it this way.
- >
- > So then I tried writing the actual code into string.cpp and compiling
- > that with and #include "string.h".
- >
- > It worked but my code for my program does not recognize the functions.
- > It says I did not code them yet.
- >
- > It finds the headers just fine but not the actual code.
- >
- > ie:
- >
- > #include "string.h"
- >
- > void main(void)
- > {
- > String blah("hello world");
- > }
- >
- > the error will say something like
- >
- > Undefined Symbol String::~String() in module STEST.CPP
- >
- > and it goes on to list the rest of the function not "defined".
- >
- > Please help...
-
- You have to link with the object code generated for your string
- class. There are several approaches to doing that. The
- simplest is to add string.cpp to your project. The compiler
- will compile that code too, and the linker will then add that
- object code to your application.
-
- While that was the simplest approach, it doesn't allow you to
- reuse string.cpp reliably. You could add string.cpp to each of
- your applications, but you would probably do so by making a
- copy. With this approach, you'd end up with many copies of
- string.cpp (and probably string.h too), so any changes you make
- must be done to every copy.
-
- The first approach to reusing the code is to put it into a
- static library, a .LIB file. You create a new project in BC to
- generate a static library, make string.cpp the one file in the
- project, then build your library. (You would probably name the
- output string.lib.) Now, if you return to your application's
- project, tell the linker to link with string.lib (be sure
- string.cpp is not part of the project). The linker will now
- locate string.lib, scan it for object files with the necessary
- code, and link that code into your application.
-
- You can get even fancier by building a DLL. A dynamic link
- library is essentially the same as a static library, except the
- "linking" stuff doesn't occur until load time. When the OS
- loads your application, it will find references to the DLL and
- will patch stub calls, to the functions in the DLL, with the
- correct address within the DLL. Once the loader is finished,
- you'll have a runtime configuration essentially the same as you
- got at link time with a static library. The DLL code
- essentially becomes part of your application while it runs.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-